//执行分页,生成页面效果,任何时候调用这个函数都会重新加载页面
function generatePage(){
    //1.获取分页数据
    var pageInfo = getPageInfoRemote();
    //2.填充表格
    fillTableBody(pageInfo);
    //

}
//远程访问服务器端程序获取pageInfo数据
function getPageInfoRemote(){
   /* $.ajax({
        "url" : "role/get/page/info.json",
        "type" : "post",
        "data" : {
            "pageNum" : window.pageNum,
            "pageSize" : window.pageSize,
            "keyword" : window.keyword
        },
        "dataType" : "json",
        "async" : false,    //开启同步
        "success" : function(response){
            pageInfo = response.data;
            fillTableBody(pageInfo);
        },
        "error": function(response){
            layer.msg("失败!状态:"+response.status+"信息:"+response.statusText)
        }
    });*/
    //调用ajax发送请求并接收返回值
    var ajaxResult = $.ajax({
        "url" : "role/get/page/info.json",
        "type" : "post",
        "data" : {
            "pageNum" : window.pageNum,
            "pageSize" : window.pageSize,
            "keyword" : window.keyword
        },
        "dataType" : "json",
        "async" : false,    //开启同步
    });
    //console.log(ajaxResult);
    //判断响应状态码是否为200
    var statusCode = ajaxResult.status;
    if(statusCode != 200){
        layer.msg("失败!状态:"+response.status+"信息:"+response.statusText);
        return  null;
    }
    //如果是200,说明是成功,获取pageInfo
    var resultEntity = ajaxResult.responseJSON;
    //从resultEntity中获取result属性
    var result = resultEntity.result;
    if(result == "FAILED"){
        layer.msg(resultEntity.message);
        return null;
    }
    var pageInfo = resultEntity.data;
    $("#summaryBox").prop("checked",false);
    return pageInfo;
}
//填充表格
function fillTableBody(pageInfo){
    //清除tbody中的旧内容
    $("#rolePageBody").empty();
    //这里清空是让没有搜索结果时没有页码导航条
    $("#Pagination").empty();
    //判断pageInfo是否有效
    if(pageInfo == null || pageInfo == undefined || pageInfo.list == null || pageInfo.list.length ==0){
        $("#rolePageBody").append("<tr><td colspan='4'>抱歉!没有查询到您要的数据</td></tr>")
        return ;
    }
    //使用pageInfo的list属性填充tbody
    for(var i = 0;i<pageInfo.list.length;i++){
        var role = pageInfo.list[i];
        var roleId = role.id;
        var roleName = role.name;
        var numberTd = "<td>"+(i+1)+"</td>";
        var checkboxTd = "<td><input class = 'itemBox' id='"+roleId+"' type='checkbox'></td>";
        var roleNameTd = "<td>"+roleName+"</td>";
        var checkBtn = "<button type =  'button' class = 'btn btn-success btn-xs'><i class='glyphicon glyphicon-check'></i></button>";
        //通过buttonb标签的id属性把roleId传递到button按钮的单击响函数
        var pencilBtn = "<button type = 'button' id='"+roleId+"' class = 'btn btn-primary btn-xs pencilBtn'><i class='glyphicon glyphicon-pencil'></i></button>";

        var removeBtn = "<button type = 'button' id='"+roleId+"' class = 'btn btn-danger btn-xs removeBtn'><i class='glyphicon glyphicon-remove'></i></button>";

        var buttonTd = "<td>"+checkBtn+" "+pencilBtn+" "+removeBtn+"</td>";

        var tr = "<tr>"+numberTd+checkboxTd+roleNameTd+buttonTd+"</tr>";

        $("#rolePageBody").append(tr);
    }

    generateNavigator(pageInfo);
}
//生成分页页码导航条
function generateNavigator(pageInfo){
    //获取总记录数
    var total = pageInfo.total;
    //声明相关属性
    var properties = {
        "num_edge_entries": 3,    //边缘页数
        "num_display_entries": 4,//主体页数
        "callback" : paginationCallBack,//指定用户点击翻页的按钮时跳转页面的回调函数
        "items_per_page" : pageInfo.pageSize, //页面显示数
        "current_page": pageInfo.pageNum -1,   //页数
        "prev_text" : "上一页",
        "next_text" : "下一页"
    }
    //调用pageInation函数
    $("#Pagination").pagination(total,properties);
}
//翻页时的回调函数
function paginationCallBack(pageIndex,jQuery){
    //修改全局变量PageNum
    window.pageNum = pageIndex+1;
    //跳转页面
   // window.location.href = "admin/get/page.html?pageNum="+pageNum+"&keyword=${param.keyword}";
    //调用分页函数
    generatePage();
    return false;
}

